using Wikey.Extensibility.Classes;
using Wikey.Extensibility.Interfaces;
using Wikey.Interfaces.Attributes;
using Wikey.Utilities.Extensions;

namespace Wikey.Transformations.Plugin
{
    [Transformer(Command="GoogleDoc")]
    public class GoogleDocTransformer : ITransformer 
    {
        public GoogleDocTransformer()
        {
            Width = 800;
            Height = 600;
        }

        public string Command
        {
            get { return this.GetAttributeValue<TransformerAttribute>().Command; }
        }

        public TransformationResult Apply(IExpressionFunction function)
        {
            var transformationResult = new TransformationResult();

            if( ! string.IsNullOrEmpty(function.Value))
            {
                this.Source = function.Value;
            }
            else if ( function.Parameters.Count > 0 )
            {
                bool first = true;
                foreach (var expressionParameter in function.Parameters)
                {
                    if (first) first = false; else {
                        this.Source += "="; }
                    this.Source += expressionParameter.Value;
                }
            }
            transformationResult.Html =
                string.Format(
                    Template , 
                    Width + "px" , 
                    Height + "px" , 
                    this.Source 
                    );

            return transformationResult;
        }

        public static string Template = @"
<iframe style='width:{0};height:{1}' src='{2}'
";
        public int Width { get; set; }

        public int Height { get; set; }

        public string Source { get; set; }

        public bool AcceptDirectParameter
        {
            get { return true; }
            set { }
        }
    }
}